package www.openteam.com.adapter.realtime;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.EvaPrtPrcBean;
import www.openteam.com.bean.EvalutateAddOrderBean;
import www.openteam.com.bean.ProductMsg;
import www.openteam.com.tool.AdpUtil;

//生产工艺
public class EvalutatePrtPrcAdp extends RecyclerView.Adapter<EvalutatePrtPrcAdp.ViewHolder> {
    Context context;
    List<EvaPrtPrcBean> beanList;
    List<String> numbList;
    List<ProductMsg.DataBeanX.DataBean> productMsgList;
    ArrayAdapter<String> productNameAdp;

    public EvalutatePrtPrcAdp(Context context) {
        this.context = context;
        this.beanList = new ArrayList<>();
        EvaPrtPrcBean evaPrtPrcBean = new EvaPrtPrcBean("0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0", "0",
                "0", "0");
        beanList.add(evaPrtPrcBean);

    }

    public List<EvaPrtPrcBean> getBeanList() {
        return beanList;
    }

    public void setNumbList(List<String> numbList) {
        this.numbList = numbList;
    }

    public void setProductMsgList(List<ProductMsg.DataBeanX.DataBean> productMsgList) {
        this.productMsgList = productMsgList;
        List<String> nameList = new ArrayList<>();
        for (ProductMsg.DataBeanX.DataBean dataBean : productMsgList) {
            nameList.add(dataBean.getName());
        }
        productNameAdp = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item, nameList);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_evalutate_product_process, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //移除TextWatch,参考https://blog.csdn.net/qq_33362988/article/details/76269448


        //提供控件显示数据对象
        EvaPrtPrcBean evaPrtPrcBean = beanList.get(position);
        //保存当前postion下item更新的数据
        EvaPrtPrcBean bean = new EvaPrtPrcBean();
        //初始化的数据，用作新增时注入
        EvaPrtPrcBean baseBean = new EvaPrtPrcBean("0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0", "0",
                "0", "0");

        AdpUtil.setEditTextWatcher(holder.evaluatePPId, s -> bean.setId(s.toString()),
                () -> bean.getId());
        AdpUtil.setEditTextWatcher(holder.evaluatePPMachineCode, s -> bean.setMachineCode(s.toString()),
                () -> bean.getMachineCode());
        AdpUtil.setEditTextWatcher(holder.evaluatePPNomalBackColor, s -> bean.setNomalBackColor(s.toString()),
                () -> bean.getNomalBackColor());
        AdpUtil.setEditTextWatcher(holder.evaluatePPNomalFrontColor, s -> bean.setNomalFrontColor(s.toString()),
                () -> bean.getNomalFrontColor());
        AdpUtil.setEditTextWatcher(holder.evaluatePPNumb, s -> bean.setNumb(s.toString()),
                () -> bean.getNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePPProcessCost, s -> bean.setProcessCost(s.toString()),
                () -> bean.getProcessCost());
        AdpUtil.setEditTextWatcher(holder.evaluatePPPart, s -> bean.setPart(s.toString()),
                () -> bean.getPart());
//        AdpUtil.setEditTextWatcher(holder.evaluatePPProcessName, s -> bean.setProcessName(s.toString()),
//                () -> bean.getProcessName());
        AdpUtil.setEditTextWatcher(holder.evaluatePPProCode, s -> bean.setProCode(s.toString()),
                () -> bean.getProCode());
        AdpUtil.setEditTextWatcher(holder.evaluatePPReserve1, s -> bean.setReserve1(s.toString()),
                () -> bean.getReserve1());
        AdpUtil.setEditTextWatcher(holder.evaluatePPReserve2, s -> bean.setReserve2(s.toString()),
                () -> bean.getReserve2());
        AdpUtil.setEditTextWatcher(holder.evaluatePPReserve3, s -> bean.setReserve3(s.toString()),
                () -> bean.getReserve3());
        AdpUtil.setEditTextWatcher(holder.evaluatePPReserve4, s -> bean.setReserve4(s.toString()),
                () -> bean.getReserve4());
        AdpUtil.setEditTextWatcher(holder.evaluatePPSerialNumb, s -> bean.setSerialNumb(s.toString()),
                () -> bean.getSerialNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePPSpotBackColor, s -> bean.setSpotBackColor(s.toString()),
                () -> bean.getSpotBackColor());
        AdpUtil.setEditTextWatcher(holder.evaluatePPSpotFrontColor, s -> bean.setSpotFrontColor(s.toString()),
                () -> bean.getSpotFrontColor());
        AdpUtil.setEditTextWatcher(holder.evaluatePPWastageNumb, s -> bean.setWastageNumb(s.toString()),
                () -> bean.getWastageNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePPWastageRate, s -> bean.setWastageRate(s.toString()),
                () -> bean.getWastageRate());
        AdpUtil.setEditTextWatcher(holder.evaluatePPWorkingCost, s -> bean.setWorkingCost(s.toString()),
                () -> bean.getWorkingCost());
        AdpUtil.setEditTextWatcher(holder.evaluatePPUnit, s -> bean.setUnit(s.toString()),
                () -> bean.getUnit());
        AdpUtil.setEditTextWatcher(holder.evaluatePPUseNumb, s -> bean.setUseNumb(s.toString()),
                () -> bean.getUseNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePPTotalNumb, s -> bean.setTotalNumb(s.toString()),
                () -> bean.getTotalNumb());

        AdpUtil.setSpinnerAdp(holder.evaluateSpPrtPrcCost, numbList, context);
        AdpUtil.setSpinnerAdp(holder.evaluateSpPrtPrcMaCost, numbList, context);

        if (productNameAdp!=null){
            holder.evaluatePPProcessName.setAdapter(productNameAdp);
        }
        holder.evaluatePPProcessName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String rate = productMsgList.get(i).getRate();
                String code = productMsgList.get(i).getCode();
                holder.evaluatePPProCode.setText(code);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        evaPrtPrcBean.setCostList(numbList);
        holder.evaluateSpPrtPrcCost.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int costNumb = Integer.valueOf(numbList.get(i));
                //使用costNumb计算得出成本和材料成本
                //赋值
                AdpUtil.setEditTextWatcher(holder.evaluatePPTotalNumb, s -> bean.setTotalNumb(s.toString()),
                        () -> bean.getTotalNumb());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        //显示最后一个新增按钮
        if (position == beanList.size() - 1) {
            holder.evaluatePPAdd.setVisibility(View.VISIBLE);
        } else {
            holder.evaluatePPAdd.setVisibility(View.GONE);
        }


        holder.evaluatePPAdd.setOnClickListener(v -> {
            if (beanList.size() <= 10) {
                //最多只能新增10个
                beanList.add(position + 1, baseBean);
                notifyDataSetChanged();
                notifyItemRemoved(position + 1);
            }
        });

        holder.evaluatePPDelete.setOnClickListener(v -> {
            if (beanList.size() > 1) {
                //留一个不删除
                beanList.remove(position);
                notifyDataSetChanged();
                notifyItemRemoved(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }
    //产品生产工艺添加的adpter

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.evaluate_PP_part)
        EditText evaluatePPPart;
        @Bind(R.id.evaluate_PP_unit)
        EditText evaluatePPUnit;
        @Bind(R.id.evaluate_PP_serialNumb)
        EditText evaluatePPSerialNumb;
        @Bind(R.id.evaluate_PP_proCode)
        EditText evaluatePPProCode;
        @Bind(R.id.evaluate_PP_workingCost)
        EditText evaluatePPWorkingCost;
        @Bind(R.id.evaluate_PP_processName)
        AppCompatSpinner evaluatePPProcessName;
        @Bind(R.id.evaluate_PP_machineCode)
        EditText evaluatePPMachineCode;
        @Bind(R.id.evaluate_PP_useNumb)
        EditText evaluatePPUseNumb;
        @Bind(R.id.evaluate_PP_wastageRate)
        EditText evaluatePPWastageRate;
        @Bind(R.id.evaluate_PP_wastageNumb)
        EditText evaluatePPWastageNumb;
        @Bind(R.id.evaluate_PP_numb)
        EditText evaluatePPNumb;
        @Bind(R.id.evaluate_PP_totalNumb)
        EditText evaluatePPTotalNumb;
        @Bind(R.id.evaluate_PP_reserve1)
        EditText evaluatePPReserve1;
        @Bind(R.id.evaluate_PP_reserve2)
        EditText evaluatePPReserve2;
        @Bind(R.id.evaluate_PP_reserve3)
        EditText evaluatePPReserve3;
        @Bind(R.id.evaluate_PP_reserve4)
        EditText evaluatePPReserve4;
        @Bind(R.id.evaluate_PP_nomalFrontColor)
        EditText evaluatePPNomalFrontColor;
        @Bind(R.id.evaluate_PP_nomalBackColor)
        EditText evaluatePPNomalBackColor;
        @Bind(R.id.evaluate_PP_spotFrontColor)
        EditText evaluatePPSpotFrontColor;
        @Bind(R.id.evaluate_PP_spotBackColor)
        EditText evaluatePPSpotBackColor;
        @Bind(R.id.evaluate_PP_id)
        EditText evaluatePPId;
        @Bind(R.id.evaluate_PP_processCost)
        EditText evaluatePPProcessCost;
        @Bind(R.id.evaluate_PP_update)
        Button evaluatePPUpdate;
        @Bind(R.id.evaluate_PP_delete)
        Button evaluatePPDelete;
        @Bind(R.id.evaluate_PP_add)
        Button evaluatePPAdd;
        @Bind(R.id.evaluate_SpPrtPrc_cost)
        Spinner evaluateSpPrtPrcCost;
        @Bind(R.id.evaluate_PrtPrc_cost)
        EditText evaluatePrtPrcCost;
        @Bind(R.id.evaluate_SpPrtPrc_MaCost)
        Spinner evaluateSpPrtPrcMaCost;
        @Bind(R.id.evaluate_PrtPrc_MaCost)
        EditText evaluatePrtPrcMaCost;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
